package com.myecl.poker.communication;


import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import com.myecl.poker.rules.holdem.HoldemRules;
import com.myecl.poker.state.Game;
import com.myecl.poker.util.Logging;


public class Main {
	final static Game game = new Game(new HoldemRules());
	
    public static void main(String[] args) throws IOException {  
    	
    	Logging.trace("Entering application...");
        startServer();
        startClient("Flo");      
        startClient("Jenny");  
        try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        game.newRound();
    }

    
    public static void startServer() throws IOException {
    	(new Thread() {   	
            @Override
            public void run() {
            	try {            		
            		ServerSocket serverSocket = new ServerSocket(9999); 
            		boolean acceptClients = true;  
    	
            		while(acceptClients) {
            			Socket socket = serverSocket.accept();
                        new ClientConnection(socket, game).start();
            		}
            		serverSocket.close();
            		
            	} catch (Exception e) {
            	 e.printStackTrace();
            	}
            }
        }).start();
    	
    }
    
    public static void startClient(final String name) {
        (new Thread() { 	
            @Override
            public void run() {
            	 Client client = new Client(name);
            }
        }).start();
    }
}